/*
 * ntppacket.h
 *
 *  Created on: 05.06.2012.
 *      Author: Aco
 *
 *      Napisano po uzoru na RFC 1305 (NTP v3) i RFC 5905 (NTP v4)
 */

#ifndef NTPPACKET_H_
#define NTPPACKET_H_

#include "types.h"

#define NTP_PORT (123)

/*
  	   0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |LI | VN  |Mode |    Stratum     |     Poll      |  Precision   |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                         Root Delay                            |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                         Root Dispersion                       |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                          Reference ID                         |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                               |
      +                     Reference Timestamp (64)                  +
      |                                                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                               |
      +                      Origin Timestamp (64)                    +
      |                                                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                               |
      +                      Receive Timestamp (64)                   +
      |                                                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                               |
      +                      Transmit Timestamp (64)                  +
      |                                                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */

typedef struct _NTPtime64S
{
	Uint32 seconds;
	Uint32 fraction;
} NTPtime64;

typedef struct _NTPtime32S
{
	Uint16 seconds;
	Uint16 fraction;
} NTPtime32;

/*
 *  Code warning of impending leap-second to be inserted at the end of
      the last day of the current month. Bits are coded as follows:

         00      no warning
         01      +1 second (following minute has 61 seconds)
         10      -1 second (following minute has 59 seconds)
         11      reserved for future use
 */

enum LIstate
{
	LI_NO_WARNING = 0,
	LI_PLUS = 1,
	LI_MINUS = 2,
	LI_RESERVED = 3
};

/*
+-------+--------------------------+
| Value | Meaning                  |
+-------+--------------------------+
| 0     | reserved                 |
| 1     | symmetric active         |
| 2     | symmetric passive        |
| 3     | client                   |
| 4     | server                   |
| 5     | broadcast                |
| 6     | NTP control message      |
| 7     | reserved for private use |
+-------+--------------------------+
*/

enum NPMode
{
	NM_RESERVED = 0,
	NM_SYMMETRIC_ACTIVE = 1,
	NM_SYMMETRIC_PASSIVE = 2,
	NM_CLIENT = 3,
	NM_SERVER = 4,
	NM_BROADCAST = 5,
	NM_CONTROL_MESSAGE = 6,
	NM_PRIVATE = 7
};

// Struktura koja tacno odgovara jednom NTP paketu od 48 bajta
typedef struct _NTPpackS
{
	Uint8 mode:3;
	Uint8 VN:3;
	Uint8 LI:2;

	Uint8 stratum;
	Uint8 poll;
	Int8 precision;

	NTPtime32 rootDelay;
	NTPtime32 rootDispersion;

	union
	{
		char name[4];
		Uint32 ipAddr;
		Uint8 ipParts[4];
	} clockIdent;

	NTPtime64 refTimestamp;
	NTPtime64 origTimestamp;
	NTPtime64 recvTimestamp;
	NTPtime64 transmitTimestamp;
} NTPpack;


#endif /* NTPPACKET_H_ */
